﻿using Abp.Application.Services.Dto;
using Abp.Application.Services;
using Choucius.Boilerplate9.OutboundPlasticContainerLogs.Dto;
using Abp.Domain.Repositories;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;

namespace Choucius.Boilerplate9.OutboundPlasticContainerLogs
{
    /// <summary>
    /// 吸塑盒出库
    /// </summary>
    public class OutboundPlasticContainerLogAppService : AsyncCrudAppService<OutboundPlasticContainerLog, OutboundPlasticContainerLogDto, long, PagedResultRequestDto, CreateOutboundPlasticContainerLogDto, CreateOutboundPlasticContainerLogDto>, IOutboundPlasticContainerLogAppService
    {
        private readonly IRepository<OutboundPlasticContainerLog, long> _outboundPlasticContainerLogRepository;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="repository"></param>
        public OutboundPlasticContainerLogAppService(IRepository<OutboundPlasticContainerLog, long> outboundPlasticContainerLogRepository) : base(outboundPlasticContainerLogRepository)
        {
            _outboundPlasticContainerLogRepository = outboundPlasticContainerLogRepository;
        }

        /// <summary>
        /// 出库统计
        /// </summary>
        /// <returns></returns>
        public async Task<List<OutboundPlasticContainerStatisticDto>> GetOutboundStatistics()
        {
            //var entities = await _logRepository.GetAllListAsync(x => x.CreationTime >= DateTime.Now.AddHours(-24));
            var entities = await _outboundPlasticContainerLogRepository.GetAllListAsync();
            //var result = entities
            //    .GroupBy(x => new { Hour = x.CreationTime.Hour / 6 }) // 分组，按每半小时分组
            //    .Select(group => new InboundStatisticDto
            //    {
            //        TimeInterval = $"{group.Key.Hour * 6}-{((group.Key.Hour + 1) * 6) - 1} hours",
            //        TotalCount = group.Sum(x => x.LabelCount)
            //    })
            //    .ToList();
            var result = entities
                .GroupBy(x => x.CreationTime.Date) // 分组，按日分组
                .Select(group => new OutboundPlasticContainerStatisticDto
                {
                    TimeInterval = $"{group.Key.Date.ToShortDateString()}",
                    TotalCount = group.Sum(x => x.LabelCount)
                })
                .ToList();
            return result;
        }
    }
}
